
** Spatial approach, 4: Convert to spmatrix format ** 

	**********------------------------------**********
	* Contents: 
		* Save data structure in spatial matrix format
		* Activate exisiting matrices needed to create spatial lags 
		* Input: neighborhood sets based on spatial_3_wmat_* 
		* Works through V-Shaped (S), Strips (B), and Intersection (C)


capture log close 
clear all
set more off 

cd "R:\WSV2\TBu_AKe\Spatial_NEW"

capture mkdir Data
global store "R:\WSV2\TBu_AKe\Spatial_NEW\Data"


log using spatial_4_spgen, replace 

	**************************
	** V-Shaped (sectors S) **
	**************************
	
cd "R:\WSV2\TBu_AKe\Spatial_NEW\Wmat_Fan"
			
/*			
***************************
*** Sectors 1-4 ***********
*** smaller j *************
***************************

use spatial_3_wmat_fan_1234 

xtset cell 
spset cell // must by spset. 

		*** S1
spmatrix fromdata S1 = s1_1 - s1_4950, normalize(none) 

spmatrix save S1 using S1.stswm, replace // inverse: spmatrix use 

		*** S2
spmatrix fromdata S2 = s2_1 - s2_4950, normalize(none) 

spmatrix save S2 using S2.stswm, replace // inverse: spmatrix use 

 
		*** S3
spmatrix fromdata S3 = s3_1 - s3_4950, normalize(none) 

spmatrix save S3 using S3.stswm, replace // inverse: spmatrix use 


		*** S4
spmatrix fromdata S4 = s4_1 - s4_4950, normalize(none) 

spmatrix save S4 using S4.stswm, replace // inverse: spmatrix use 


clear 

	
			***************************
			*** Sectors 5-8
			*** 120 to 45 
			***************************
			
use spatial_3_wmat_fan_5678

xtset cell 
spset cell // must by spset. 

		*** S5
spmatrix fromdata S5 = s5_1 - s5_4950, normalize(none) 

spmatrix save S5 using S5.stswm, replace // inverse: spmatrix use 


		*** S6
spmatrix fromdata S6 = s6_1 - s6_4950, normalize(none) 

spmatrix save S6 using S6.stswm, replace // inverse: spmatrix use 

 
		*** S7
spmatrix fromdata S7 = s7_1 - s7_4950, normalize(none) 

spmatrix save S7 using S7.stswm, replace // inverse: spmatrix use 


		*** S8
spmatrix fromdata S8 = s8_1 - s8_4950, normalize(none) 

spmatrix save S8 using S8.stswm, replace // inverse: spmatrix use 


clear 	

*/

	** S1 and 2: sw quadrant 
spmatrix use S1 using S1.stswm, replace	
spmatrix use S2 using S2.stswm, replace	

	** S3 to S6: nw quadrant 
spmatrix use S3 using S3.stswm, replace	
spmatrix use S4 using S4.stswm, replace	

spmatrix use S5 using S5.stswm, replace	
spmatrix use S6 using S6.stswm, replace	

	** S7 and 8: no quadrant 
spmatrix use S7 using S7.stswm, replace	
spmatrix use S8 using S8.stswm, replace	 
 

clear 
 
 
	****************************************
	*** Intersection (4 corners of cell) ***
	****************************************

	
cd "R:\WSV2\TBu_AKe\Spatial_NEW\Wmat_4corners_reduced"

			
***************************
*** Sectors 1-4 ***********
*** smaller j *************
***************************
/* 
use spatial_3_wmat_4corners_1234 

xtset cell 
spset cell // must by spset. 

		*** S1
spmatrix fromdata C1 = s1_1 - s1_4950, normalize(none) 

spmatrix save C1 using C1.stswm, replace // inverse: spmatrix use 

		*** C2
spmatrix fromdata C2 = s2_1 - s2_4950, normalize(none) 

spmatrix save C2 using C2.stswm, replace // inverse: spmatrix use 

 
		*** C3
spmatrix fromdata C3 = s3_1 - s3_4950, normalize(none) 

spmatrix save C3 using C3.stswm, replace // inverse: spmatrix use 


		*** C4
spmatrix fromdata C4 = s4_1 - s4_4950, normalize(none) 

spmatrix save C4 using C4.stswm, replace // inverse: spmatrix use 


clear 

	
			***************************
			*** Sectors 5-8
			*** 120 to 45 
			***************************
			
use spatial_3_wmat_4corners_5678

xtset cell 
spset cell // must by spset. 

		*** C5
spmatrix fromdata C5 = s5_1 - s5_4950, normalize(none) 

spmatrix save C5 using C5.stswm, replace // inverse: spmatrix use 


		*** C6
spmatrix fromdata C6 = s6_1 - s6_4950, normalize(none) 

spmatrix save C6 using C6.stswm, replace // inverse: spmatrix use 

 
		*** C7
spmatrix fromdata C7 = s7_1 - s7_4950, normalize(none) 

spmatrix save C7 using C7.stswm, replace // inverse: spmatrix use 


		*** C8
spmatrix fromdata C8 = s8_1 - s8_4950, normalize(none) 

spmatrix save C8 using C8.stswm, replace // inverse: spmatrix use 


clear 	

*/

	** C1 and 2: sw quadrant 
spmatrix use C1 using C1.stswm, replace	
spmatrix use C2 using C2.stswm, replace	

	** C3 to C6: nw quadrant 
spmatrix use C3 using C3.stswm, replace	
spmatrix use C4 using C4.stswm, replace	

spmatrix use C5 using C5.stswm, replace	
spmatrix use C6 using C6.stswm, replace	

	** C7 and 8: no quadrant 
spmatrix use C7 using C7.stswm, replace	
spmatrix use C8 using C8.stswm, replace	

clear 

		*******************************
		***    Strips (beam B)  	***
		*******************************
			

cd "R:\WSV2\TBu_AKe\Spatial_NEW\Wmat_Beam"

		
/* 			
***************************
*** Sectors 1-4 ***********
*** smaller j *************
***************************

use spatial_3_wmat_beam_1234 

xtset cell 
spset cell // must by spset. 

		*** B1
spmatrix fromdata B1 = s1_1 - s1_4950, normalize(none) 

spmatrix save B1 using B1.stswm, replace // inverse: spmatrix use 

		*** B2
spmatrix fromdata B2 = s2_1 - s2_4950, normalize(none) 

spmatrix save B2 using B2.stswm, replace // inverse: spmatrix use 

 
		*** B3
spmatrix fromdata B3 = s3_1 - s3_4950, normalize(none) 

spmatrix save B3 using B3.stswm, replace // inverse: spmatrix use 


		*** B4
spmatrix fromdata B4 = s4_1 - s4_4950, normalize(none) 

spmatrix save B4 using B4.stswm, replace // inverse: spmatrix use 


clear 

			***************************
			*** Sectors 5-8  
			*** 120 to 45 
			***************************
			
use spatial_3_wmat_beam_5678

xtset cell 
spset cell // must by spset. 

		*** B5
spmatrix fromdata B5 = s5_1 - s5_4950, normalize(none) 

spmatrix save B5 using B5.stswm, replace // inverse: spmatrix use 


		*** B6
spmatrix fromdata B6 = s6_1 - s6_4950, normalize(none) 

spmatrix save B6 using B6.stswm, replace // inverse: spmatrix use 

 
		*** B7
spmatrix fromdata B7 = s7_1 - s7_4950, normalize(none) 

spmatrix save B7 using B7.stswm, replace // inverse: spmatrix use 


		*** B8
spmatrix fromdata B8 = s8_1 - s8_4950, normalize(none) 

spmatrix save B8 using B8.stswm, replace // inverse: spmatrix use 


clear 	

*/ 

	** B1 and 2: sw quadrant 
spmatrix use B1 using B1.stswm, replace	
spmatrix use B2 using B2.stswm, replace	

	** B3 to B6: nw quadrant 
spmatrix use B3 using B3.stswm, replace	
spmatrix use B4 using B4.stswm, replace	

spmatrix use B5 using B5.stswm, replace	
spmatrix use B6 using B6.stswm, replace	

	** B7 and 8: no quadrant 
spmatrix use B7 using B7.stswm, replace	
spmatrix use B8 using B8.stswm, replace	

clear

 

*********************************************
* Transform weight matrix to Finished Data **
*********************************************

cd "C:\Users\hy65byfe\Desktop\smerge_0712"
use spatial_6_regs_s_monthly, replace

drop if missing(date) // from collapse structure 

*** spset 
xtset cell kt // kt: cntry-year observation: must be balanced 
capture spset cell  // set identifier


******** Outcome: sales 
		
replace L3_sd = 0 if missing(L3_sd) // set missing to zero in pre-ban sales 
replace L3_lsd = 0 if missing(L3_lsd)  // 

		** contemp. 
replace log_sd = 0 if missing(log_sd) // 

******** Outcome: count 

replace L3_cd = 0 if missing(L3_cd) // 
replace L3_lcd = 0 if missing(L3_lcd)  //  
		
		** contemp. 
replace log_cd = 0 if missing(log_cd)


******** Load Matrices
			
spmatrix dir // display active matrices. 

******** Gen Lags 

 forvalues i = 1/8 {
	gen S`i'_L3_sd = 0 
	gen S`i'_L3_cd = 0 
	
	gen C`i'_L3_sd = 0 
	gen C`i'_L3_cd = 0 
	
	gen B`i'_L3_sd = 0 
	gen B`i'_L3_cd = 0 
}
 

keep if year == 2014 // reduce data in memory to neccessary.  

egen k14 = group(ccode date) // country-date groups  	

drop if missing(date) // 
	
sum k14, meanonly  // date ccode group. 
scalar kmin = `r(min)' // store in scalars 
scalar kmax = `r(max)'
	
scalar list kmin kmax // 1 to 84 
 
 *** spset to verify structure 
xtset cell k14 // kt: cntry-year observation: must be balanced.
spset, clear 
spset cell  // set identifier


 ******  Create spatial variables for 1 - 8 
 
foreach var of varlist L3_sd L3_cd {
 	forvalues i = `=kmin'/`=kmax' {
	local k = `i' 
		
      spgenerate Waux_`var'_`i' = S1*`var' if k14==`k'
         quietly replace S1_`var'=Waux_`var'_`i' if k14==`k'
		 drop Waux* 
		 
	   spgenerate Waux_`var'_`i' = C1*`var' if k14==`k'
         quietly replace C1_`var'=Waux_`var'_`i' if k14==`k'
		 drop Waux* 
		 
		spgenerate Waux_`var'_`i' = B1*`var' if k14==`k'
         quietly replace B1_`var'=Waux_`var'_`i' if k14==`k'
		 drop Waux* 
         } 
 } 
 
 
 foreach var of varlist L3_sd L3_cd {
 	forvalues i = `=kmin'/`=kmax' {
	local k = `i' 
		
      spgenerate Waux_`var'_`i' = S2*`var' if k14==`k'
         quietly replace S2_`var'=Waux_`var'_`i' if k14==`k'
		 drop Waux* 
		 
	   spgenerate Waux_`var'_`i' = C2*`var' if k14==`k'
         quietly replace C2_`var'=Waux_`var'_`i' if k14==`k'
		 drop Waux* 
		 
		spgenerate Waux_`var'_`i' = B2*`var' if k14==`k'
         quietly replace B2_`var'=Waux_`var'_`i' if k14==`k'
		 drop Waux* 
         } 
 } 
 
 
  foreach var of varlist L3_sd L3_cd {
 	forvalues i = `=kmin'/`=kmax' {
	local k = `i' 
      spgenerate Waux_`var'_`i' = S3*`var' if k14==`k'
         quietly replace S3_`var'=Waux_`var'_`i' if k14==`k'
		 drop Waux* 
		 
	   spgenerate Waux_`var'_`i' = C3*`var' if k14==`k'
         quietly replace C3_`var'=Waux_`var'_`i' if k14==`k'
		 drop Waux* 
		 
		spgenerate Waux_`var'_`i' = B3*`var' if k14==`k'
         quietly replace B3_`var'=Waux_`var'_`i' if k14==`k'
		 drop Waux* 
         } 
 } 
 
 
 
  foreach var of varlist L3_sd L3_cd {
 	forvalues i = `=kmin'/`=kmax' {
	local k = `i' 
		
      spgenerate Waux_`var'_`i' = S4*`var' if k14==`k'
         quietly replace S4_`var'=Waux_`var'_`i' if k14==`k'
		 drop Waux* 
		 
	   spgenerate Waux_`var'_`i' = C4*`var' if k14==`k'
         quietly replace C4_`var'=Waux_`var'_`i' if k14==`k'
		 drop Waux* 
		 
		spgenerate Waux_`var'_`i' = B4*`var' if k14==`k'
         quietly replace B4_`var'=Waux_`var'_`i' if k14==`k'
		 drop Waux* 
         } 
 } 
 
 
 foreach var of varlist L3_sd L3_cd {
 	forvalues i = `=kmin'/`=kmax' {
	local k = `i' 
		
      spgenerate Waux_`var'_`i' = S5*`var' if k14==`k'
         quietly replace S5_`var'=Waux_`var'_`i' if k14==`k'
		 drop Waux* 
		 
	   spgenerate Waux_`var'_`i' = C5*`var' if k14==`k'
         quietly replace C5_`var'=Waux_`var'_`i' if k14==`k'
		 drop Waux* 
		 
		spgenerate Waux_`var'_`i' = B5*`var' if k14==`k'
         quietly replace B5_`var'=Waux_`var'_`i' if k14==`k'
		 drop Waux* 
         } 
 } 
 
 
 foreach var of varlist L3_sd L3_cd {
 	forvalues i = `=kmin'/`=kmax' {
	local k = `i' 
		
      spgenerate Waux_`var'_`i' = S6*`var' if k14==`k'
         quietly replace S6_`var'=Waux_`var'_`i' if k14==`k'
		 drop Waux* 
		 
	   spgenerate Waux_`var'_`i' = C6*`var' if k14==`k'
         quietly replace C6_`var'=Waux_`var'_`i' if k14==`k'
		 drop Waux* 
		 
		spgenerate Waux_`var'_`i' = B6*`var' if k14==`k'
         quietly replace B6_`var'=Waux_`var'_`i' if k14==`k'
		 drop Waux* 
         } 
 } 

 
 
foreach var of varlist L3_sd L3_cd {
 	forvalues i = `=kmin'/`=kmax' {
	local k = `i' 
		
      spgenerate Waux_`var'_`i' = S7*`var' if k14==`k'
         quietly replace S7_`var'=Waux_`var'_`i' if k14==`k'
		 drop Waux* 
		 
	   spgenerate Waux_`var'_`i' = C7*`var' if k14==`k'
         quietly replace C7_`var'=Waux_`var'_`i' if k14==`k'
		 drop Waux* 
		 
		spgenerate Waux_`var'_`i' = B7*`var' if k14==`k'
         quietly replace B7_`var'=Waux_`var'_`i' if k14==`k'
		 drop Waux* 
         } 
 }  
 
 
 
foreach var of varlist L3_sd L3_cd {
 	forvalues i = `=kmin'/`=kmax' {
	local k = `i' 
		
      spgenerate Waux_`var'_`i' = S8*`var' if k14==`k'
         quietly replace S8_`var'=Waux_`var'_`i' if k14==`k'
		 drop Waux* 
		 
	   spgenerate Waux_`var'_`i' = C8*`var' if k14==`k'
         quietly replace C8_`var'=Waux_`var'_`i' if k14==`k'
		 drop Waux* 
		 
		spgenerate Waux_`var'_`i' = B8*`var' if k14==`k'
         quietly replace B8_`var'=Waux_`var'_`i' if k14==`k'
		 drop Waux* 
         } 
 }  
 
 
*browse cell year ccode S* C* B* L3_sd L3_cd if cell <= 50 & year == 2014 // 
cd "$store"
save spatial_regs_s_monthly, replace
